package com.android.v1.ordertracking.utils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class JsonUtil<E> {
	public <T> T parseModelFromJson(String jsonData, Class<T> classOfT,
			String rubyClassName) {
		T t = null;
		try {
			JSONObject obj = new JSONObject(jsonData);
			Log.i("sysout", rubyClassName + "||");
			Log.i("sysout", obj.getJSONObject(rubyClassName).toString());
			Gson gson = new Gson();
			if (rubyClassName != null) {
				t = gson.fromJson(obj.getJSONObject(rubyClassName).toString(), (Type) classOfT);
			} else {
				t = gson.fromJson(obj.toString(), (Type) classOfT);
			}
			Log.i("sysout", t.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	/**
	 * 
	 * @param jsonDate T的对象的json，格式为：{"head_url":null,"id":3,"created_at":"2012-08-27 00:00:00","user_id":3,"user_type":1,"name":"wanglei"}
	 * @param classOfT json对象的类，如：User.class
	 * @return
	 */
	public <T> T parseModelFromJson(String jsonDate, Class<T> classOfT){
		T t = null;
		Gson gson = new Gson();
		t = gson.fromJson(jsonDate, (Type) classOfT);
		return t;
	}
	
	/**
	 * 
	 * @param jsonData 
	 * @param key
	 * @return
	 */
	public String parseStringFomrJson(String jsonData, String key){
		JSONObject obj;
		String t = null;
		try {
			obj = new JSONObject(jsonData);
			t = obj.getString(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}	
		return t;
	}
	

	/**
	 * 
	 * @param jsonData
	 * @param E 数组中json对象的类，如：User.class
	 * @param key JSONArray str的key
	 * 
	 * str json对象的数组，格式为[{"head_url":null,"id":3,"created_at":"2012-08-27 00:00:00","user_id":3,"user_type":1,"name":"wanglei"},
	 * {"head_url":null,"id":3,"created_at":"2012-08-27 00:00:00","user_id":3,"user_type":1,"name":"wanglei"}]
	 * @return
	 */
	public ArrayList<E> parseArrayFromJson(String jsonData, Class<E> E, String key){
		String str = this.parseStringFomrJson(jsonData, key);
		ArrayList<E> t = new ArrayList<E>();
		try {
			JSONArray arr = new JSONArray(str);
			for (int i = 0; i < arr.length(); i++) {
				E obj = this.parseModelFromJson(arr.getString(i), E);
				t.add(obj);
			}
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	public ArrayList<E> parseJosnArray(String jsonData, String key, String rubyClassName, Class<E> E) {
		Log.i("sysout", "jsonSource: " + jsonData);
		JSONObject obj;
		ArrayList<E> t = new ArrayList<E>();
		try {
			obj = new JSONObject(jsonData);
			JSONArray arr = obj.getJSONArray(key);
			Log.i("sysout", rubyClassName + "||" + arr.length());
			t = this.parseArrayFromJson(arr, rubyClassName, E);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return t;
	}

	public ArrayList<E> parseArrayFromJson(JSONArray obj, String rubyClassName, Class<E> E) {
		ArrayList<E> t = new ArrayList<E>();
		try {
			for (int i = 0; i < obj.length(); i++) {
				E mp = (E) this.parseModelFromJson(obj.getJSONObject(i)
						.toString(), E, rubyClassName);
				t.add(mp);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	public ArrayList<String> parseJsonArray(JSONArray arr, String key){
		ArrayList<String> t = new ArrayList<String>();
		try {
			for (int i = 0; i < arr.length(); i++) {
				JSONObject obj = arr.getJSONObject(i);
				t.add(obj.getString(key));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return t;
	}
	
//	public String fromJson(String jsonData, String key){
//		JSONObject obj;
//		String t = null;
//		try {
//			obj = new JSONObject(jsonData);
//			t = obj.getString(key);
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}	
//		return t;
//	}
	
}
